
<h1><span class="yiyi-st" id="yiyi-12">numpy.expand_dims</span></h1>
        <blockquote>
        <p>原文：<a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.expand_dims.html">https://docs.scipy.org/doc/numpy/reference/generated/numpy.expand_dims.html</a></p>
        <p>译者：<a href="https://github.com/wizardforcel">飞龙</a> <a href="http://usyiyi.cn/">UsyiyiCN</a></p>
        <p>校对：（虚位以待）</p>
        </blockquote>
    
<dl class="function">
<dt id="numpy.expand_dims"><span class="yiyi-st" id="yiyi-13"> <code class="descclassname">numpy.</code><code class="descname">expand_dims</code><span class="sig-paren">(</span><em>a</em>, <em>axis</em><span class="sig-paren">)</span><a class="reference external" href="http://github.com/numpy/numpy/blob/v1.11.3/numpy/lib/shape_base.py#L219-L274"><span class="viewcode-link">[source]</span></a></span></dt>
<dd><p><span class="yiyi-st" id="yiyi-14">展开数组的形状。</span></p>
<p><span class="yiyi-st" id="yiyi-15">插入新轴，对应于数组形状中的给定位置。</span></p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name">
<col class="field-body">
<tbody valign="top">
<tr class="field-odd field"><th class="field-name"><span class="yiyi-st" id="yiyi-16">参数：</span></th><td class="field-body"><p class="first"><span class="yiyi-st" id="yiyi-17"><strong>a</strong>：array_like</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-18">输入数组。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-19"><strong>axis</strong>：int</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-20">插入新轴的位置（轴之间）。</span></p>
</div></blockquote>
</td>
</tr>
<tr class="field-even field"><th class="field-name"><span class="yiyi-st" id="yiyi-21">返回：</span></th><td class="field-body"><p class="first"><span class="yiyi-st" id="yiyi-22"><strong>res</strong>：ndarray</span></p>
<blockquote class="last">
<div><p><span class="yiyi-st" id="yiyi-23">输出数组。</span><span class="yiyi-st" id="yiyi-24">维数的数量大于输入数组的数量。</span></p>
</div></blockquote>
</td>
</tr>
</tbody>
</table>
<div class="admonition seealso">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-25">也可以看看</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-26"><a class="reference internal" href="../../user/basics.indexing.html#module-numpy.doc.indexing" title="numpy.doc.indexing"><code class="xref py py-obj docutils literal"><span class="pre">doc.indexing</span></code></a>，<a class="reference internal" href="numpy.atleast_1d.html#numpy.atleast_1d" title="numpy.atleast_1d"><code class="xref py py-obj docutils literal"><span class="pre">atleast_1d</span></code></a>，<a class="reference internal" href="numpy.atleast_2d.html#numpy.atleast_2d" title="numpy.atleast_2d"><code class="xref py py-obj docutils literal"><span class="pre">atleast_2d</span></code></a>，<a class="reference internal" href="numpy.atleast_3d.html#numpy.atleast_3d" title="numpy.atleast_3d"><code class="xref py py-obj docutils literal"><span class="pre">atleast_3d</span></code></a></span></p>
</div>
<p class="rubric"><span class="yiyi-st" id="yiyi-27">例子</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="o">.</span><span class="n">shape</span>
<span class="go">(2,)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-28">以下等效于<code class="docutils literal"><span class="pre">x[np.newaxis,:]</span></code>或<code class="docutils literal"><span class="pre">x[np.newaxis]</span></code>：</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span>
<span class="go">array([[1, 2]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span><span class="o">.</span><span class="n">shape</span>
<span class="go">(1, 2)</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>  <span class="c1"># Equivalent to x[:,newaxis]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span>
<span class="go">array([[1],</span>
<span class="go">       [2]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span><span class="o">.</span><span class="n">shape</span>
<span class="go">(2, 1)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-29">请注意，一些示例可以使用<code class="docutils literal"><span class="pre">None</span></code>而不是<code class="docutils literal"><span class="pre">np.newaxis</span></code>。</span><span class="yiyi-st" id="yiyi-30">这些是相同的对象：</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">newaxis</span> <span class="ow">is</span> <span class="kc">None</span>
<span class="go">True</span>
</pre></div>
</div>
</dd></dl>
